package cn.lblbc.downloadlib.database;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

@Dao
public interface DownloadChunkDao {
    @Query("SELECT * FROM download_chunks WHERE file_id = :fileId")
    List<DownloadChunkEntity> getChunksByFileId(long fileId);

    @Query("SELECT * FROM download_chunks WHERE chunk_id = :chunkId")
    DownloadChunkEntity getChunkById(long chunkId);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertChunk(DownloadChunkEntity chunk);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertChunks(List<DownloadChunkEntity> chunks);

    @Update
    void updateChunk(DownloadChunkEntity chunk);

    @Query("DELETE FROM download_chunks WHERE file_id = :fileId")
    void deleteChunksByFileId(long fileId);

    @Query("DELETE FROM download_chunks WHERE chunk_id = :chunkId")
    void deleteChunk(long chunkId);

    @Query("UPDATE download_chunks SET downloaded_size = :downloadedSize, update_time = :updateTime WHERE chunk_id = :chunkId")
    void updateChunkProgress(long chunkId, long downloadedSize, long updateTime);
}